package com.sanshi.project.procure.controller;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sanshi.project.admin.api.dto.ProcureGoodsDTO;
import com.sanshi.project.admin.api.entity.BuildingEntity;
import com.sanshi.project.admin.api.entity.ProcureGoodsEntity;
import com.sanshi.project.common.core.util.R;
import com.sanshi.project.common.excel.annotation.ResponseExcel;
import com.sanshi.project.common.log.annotation.SysLog;
import com.sanshi.project.procure.service.ProcureGoodsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物品信息
 *
 * @author simon
 * @date 2023-12-08 21:18:40
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/procureGoods" )
@Tag(description = "procureGoods" , name = "物品信息管理" )
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class ProcureGoodsController {

    private final  ProcureGoodsService procureGoodsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param procureGoods 物品信息
     * @return
     */
    @Operation(summary = "分页查询" , description = "分页查询" )
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('procure_procureGoods_view')" )
    public R getProcureGoodsPage(@ParameterObject Page page, @ParameterObject ProcureGoodsDTO procureGoods) {
        LambdaQueryWrapper<ProcureGoodsEntity> wrapper = Wrappers.lambdaQuery();
		// 物品名称
		wrapper.like(StrUtil.isNotBlank(procureGoods.getGoodsName()), ProcureGoodsEntity::getGoodsName, procureGoods.getGoodsName());
		wrapper.eq(StrUtil.isNotBlank(procureGoods.getWarehouseId()), ProcureGoodsEntity::getWarehouseId, procureGoods.getWarehouseId());
		wrapper.eq(StrUtil.isNotBlank(procureGoods.getGoodsType()), ProcureGoodsEntity::getGoodsType, procureGoods.getGoodsType());
        wrapper.orderByDesc(ProcureGoodsEntity::getUpdateTime);
        return R.ok(procureGoodsService.page(page, wrapper));
    }


    /**
     * 通过id查询物品信息
     * @param id id
     * @return R
     */
    @Operation(summary = "通过id查询" , description = "通过id查询" )
    @GetMapping("/{id}" )
    @PreAuthorize("@pms.hasPermission('procure_procureGoods_view')" )
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(procureGoodsService.getById(id));
    }

    /**
     * 新增物品信息
     * @param procureGoods 物品信息
     * @return R
     */
    @Operation(summary = "新增物品信息" , description = "新增物品信息" )
    @SysLog("新增物品信息" )
    @PostMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoods_add')" )
    public R save(@RequestBody ProcureGoodsEntity procureGoods) {
		procureGoods.setGoodsInventory(0);
        return R.ok(procureGoodsService.save(procureGoods));
    }

    /**
     * 修改物品信息
     * @param procureGoods 物品信息
     * @return R
     */
    @Operation(summary = "修改物品信息" , description = "修改物品信息" )
    @SysLog("修改物品信息" )
    @PutMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoods_edit')" )
    public R updateById(@RequestBody ProcureGoodsEntity procureGoods) {
        return R.ok(procureGoodsService.updateById(procureGoods));
    }

    /**
     * 通过id删除物品信息
     * @param ids id列表
     * @return R
     */
    @Operation(summary = "通过id删除物品信息" , description = "通过id删除物品信息" )
    @SysLog("通过id删除物品信息" )
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('procure_procureGoods_del')" )
    public R removeById(@RequestBody Long[] ids) {
        return R.ok(procureGoodsService.removeBatchByIds(CollUtil.toList(ids)));
    }


    /**
     * 导出excel 表格
     * @param procureGoods 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('procure_procureGoods_export')" )
    public List<ProcureGoodsEntity> export(ProcureGoodsEntity procureGoods, Long[] ids) {
        return procureGoodsService.list(Wrappers.lambdaQuery(procureGoods).in(ArrayUtil.isNotEmpty(ids), ProcureGoodsEntity::getId, ids));
    }
}